package de.ars.client.screens;

import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;

import de.ars.client.Main;
import de.ars.client.datahandling.DataMangementSystem;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.tools.Factory;
import de.ars.common.IDataModelConstants;

public class LoginScreen extends Form implements ActionListener
{
    private Main parent;
    private String savedPin = null;
    private boolean b_showWrongPin = false;
    private boolean b_showPinsNotEqual = false;
    private Container mta_pin1 = null;
    private Container mta_pin2 = null;
    private String toShowString = null;

    private static final int SHOW_STRING_TIME = 500;

    private class MyTextArea extends TextArea
    {
        private String pin = "";
        
        public MyTextArea()
        {
            this.setFocusable( true );
        }

        public void keyPressed( int keyCode )
        {
            boolean b_cunsumed = false;
            
            switch( keyCode )
            {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    if( this.getText().length() < 4 )
                    {
                        this.setText( this.getText() + '*' );
                        pin += (char)keyCode;
                    }
                    b_cunsumed = true;
                    break;
                case -8:
                    if( this.getText().length() > 0 )
                    {
                        this.setText( this.getText().substring( 0, this.getText().length() - 1 ) );
                        pin = pin.substring( 0, pin.length() - 1 );
                    }
                    b_cunsumed = true;
                    break;
            }
            if( !b_cunsumed )
                super.keyPressed( keyCode );
            toShowString = "keyCode: " + new Integer(keyCode).toString();
            LoginScreen.this.repaint();
        }
        
        public String getPin()
        {
            return this.pin;
        }
    }

    public LoginScreen( Main parent, String title )
    {
        setTitle( title );
        this.parent = parent;
        addContent();
    }

    private void addContent()
    {
        this.setLayout( new BoxLayout( BoxLayout.Y_AXIS ) );
        this.addComponent( Factory.createTextArea( PropertyReader.getProperty( "local.welcome" ), false, false ) );
        byte[] b_temp = DataMangementSystem.loadDataFromRS( IDataModelConstants.DATAMODEL_PIN );
        if( b_temp != null )
            this.savedPin = new String( b_temp );

        if( this.savedPin != null )
            showEnterPinItems();
        else
            showCreateNewPinItems();
        
        Factory.addExitButton( this );
        Factory.addNextButton( this );
        this.addCommandListener( this );

        //Debugging
        //DataMangementSystem.saveData( IDataModelConstants.DATAMODEL_PIN, "1234".getBytes() );
    }

    private void showEnterPinItems()
    {
        this.addComponent( Factory.createTextArea( PropertyReader.getProperty( "local.pin" ), false, false ) );
        this.addComponent( mta_pin1 = Factory.createInfoPair( "Pin: ", new MyTextArea(), parent ) );
    }

    private void showCreateNewPinItems()
    {
        this.addComponent( Factory.createTextArea( PropertyReader.getProperty( "local.newPin" ), false, false ) );
        this.addComponent( mta_pin1 = Factory.createInfoPair( "Pin: ", new MyTextArea(), parent ) );
        this.addComponent( mta_pin2 = Factory.createInfoPair( "Confirm Pin: ", new MyTextArea(), parent ) );
    }

    public void actionPerformed( ActionEvent evt )
    {
        Command cmd_temp = evt.getCommand();
        switch( cmd_temp.getId() )
        {
            case IDataModelConstants.EXIT_COMMAND:
                parent.notifyDestroyed();
                break;
            case IDataModelConstants.NEXT_COMMAND:
                if( this.savedPin == null )
                {
                    
                    if( ((MyTextArea)mta_pin1.getComponentAt( 1 )).getPin().equals( ((MyTextArea)mta_pin2.getComponentAt( 1 )).getPin() ) )
                    {
                        DataMangementSystem.saveDataToRS( IDataModelConstants.DATAMODEL_PIN, ((MyTextArea)mta_pin1.getComponentAt( 1 )).getPin().getBytes() );
                        parent.showScreen( IDataModelConstants.SCREEN_MAIN );
                    }
                    else
                    {
                        b_showPinsNotEqual = true;
                        repaint();
                    }
                }
                else
                {
                    if( this.savedPin.equals( ((MyTextArea)mta_pin1.getComponentAt( 1 )).getPin() ) )
                        parent.showScreen( IDataModelConstants.SCREEN_MAIN );
                    else
                    {
                        if( b_showWrongPin == false )
                        {
                            b_showWrongPin = true;
                            repaint();
                        }
                    }
                }
                break;
        }
    }

    public void paint( Graphics g )
    {
        super.paint( g );
        
        if( b_showWrongPin )
        {
            toShowString = PropertyReader.getProperty( "local.wrongPin" );
            b_showWrongPin = false;
        }
        else if (b_showPinsNotEqual)
        {
            toShowString = PropertyReader.getProperty( "local.pinsNotEqual" );
            b_showPinsNotEqual = false;
        }
        
       /* if( toShowString != null )
        {
            g.drawString(toShowString , this.getWidth() / 2 - 40,
                    this.getHeight() / 2 - 5 );
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        Thread.sleep( SHOW_STRING_TIME );
                    }
                    catch( InterruptedException e )
                    {
                        e.printStackTrace();
                    }
                    
                    repaint();
                }
            }.start();
        }*/
    }
}
